<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<meta charset='utf-8'>
    <title></title>

    <style type="text/css">
        body{ text-align:center}
        #div_random
        {
            height: 40px;
            width: 341px;
        }
        #div_result
        {
           min-height: 100px;
            width: 341px;
        }
        .style1
        {
            text-align: center;
            width: 100%;
        }
        .style2
        {
            text-align: center;
            width: 349px;
        }
    </style>
</head>
<body >
    <form id="form1" runat="server">
<table  class="style1">
            <tr>
                <td class="style2">  
                  <div id="div_random" 
        
                        style="margin:0 auto;font-family: 方正姚体; font-size: 40px; font-style: inherit; font-variant: normal; color: #0099FF; background-color: #99FF99; text-align: center;">摇号
        
    </div>
                    </td>
                </tr>
                <tr>
                    <td>
                         <div id="div_result" 
        
                        style="margin:0 auto;font-family: 方正姚体; font-size: 40px;  font-style: inherit; font-variant: normal; color: #0099FF; background-color: #ffffff; text-align: center;">摇号结果
        
    </div>
                    </td>
                </tr>
                    <tr>
                <td>
                    <input type="button" value="开始摇号" onclick="Start()" id="btn_start" 
                        style="font-family: 方正姚体;font-size:20px; font-style: inherit; color: #FF0000; background-color: #FFFFCC"/>
                        <input type="button" value="停    止" onclick="Stop()" id="btn_stop" style=" display:none;font-family: 方正姚体;font-size:20px; font-style: inherit; color: #FF0000; background-color: #FFFFCC"/>
                        <input type="text" value="1" id="result_number"/>
                    </td>
               
                </td>
            </tr>
        </table>
    <div>
   
    </div>
        <script type="text/javascript">
            var int;
            <?php

                $fp = fopen('total.txt', 'r');
                if (!$fp) {
                    echo 'Could not open file total.txt';
                }
                $total = array();
                while ($char = fgets($fp)) {
                     $total[]=trim($char);
                }
                $total_str ="[".implode(",", $total)."]";

                $fp1 = fopen('real.txt', 'r');
                if (!$fp) {
                    echo 'Could not open file real.txt';
                }
                $real = array();
                while ($char = fgets($fp1)) {
                     $real[]=trim($char);
                }
                $real_str ="[".implode(",", $real)."]";

             ?>
            var randYes=<?php echo $total_str;?>;
            var randW = <?php echo $real_str;?>;
            var randomNum=<?php echo count($total);?> ;
            var speed=5;
            var num=0;
            var once=new Array();
            var once_num =1;
            var result=new Array();
            function setparm() {
                randomNum = parseInt(document.getElementById("txt_randomNum").value);
                speed = parseInt(document.getElementById("txt_speed").value);
                alert("设置成功！");
            }
            function random() {
               // once = new Array();
                var ind=parseInt(Math.random()*randomNum);
                if(ind==10){
                    ind =0;
                }
               var r=randYes[ind];
               document.getElementById("div_random").innerHTML = r;
                console.log(once.length);
                console.log(once[once.length-1])
                console.log(ind+"--start:--"+r);
                for(var i=once.length;i<once_num;i++)
                {
                 var flag=0;
                 do
                 {
                  for(var j=0;j<result.length;j++)
                  {
                   if(result[j]==r) {flag=1;break;}
                  }
                  for(var j=0;j<once.length;j++)
                  {
                   if(once[j]==r) {flag=1;break;}
                  }
                  if(!flag) 
                  {
                   once.push(r);
                   console.log(ind+"--push:---"+r);
                   break;
                  }
                  else
                  {
                   var ind=parseInt(Math.random()*randomNum);
                    if(ind==10){
                        ind =0;
                    }

                    r=randYes[ind];
                    console.log(ind+"--middle:---"+r);
                    flag = 0;
                  }
                 }while(!flag);
                }
                
                
            }
            function Start() {
                once = new Array();
                once_num = document.getElementById("result_number").value;
                if(result.length+parseInt(once_num)>randomNum){
                    alert("数据不足！");
                    return;
                }
                for(var j=0;j<once_num;j++){
                    for(var i=0;i<randW.length;i++){
                        var num1 = randW[i];
                        var flag=0;
                        for(var k=0;k<result.length;k++)
                        {
                         if(result[k]==num1) {flag=1;break;}
                        }
                        for(var k=0;k<once.length;k++)
                        {
                         if(once[k]==num1) {flag=1;break;}
                        }
                        if(!flag){
                            once.push(randW[i]);
                            break;
                        }
                    }

                }
                
                int = setInterval(random, speed);
                document.getElementById("btn_start").style.display = "none";
                document.getElementById("btn_stop").style.display = "";
            }
            function Stop() {
                clearInterval(int);
                for(var i=0;i<once.length;i++){
                    result.push(once[i]);
                }
                var text="";
                for (var i = 0; i < result.length; i++) {
                     text+="["+(i+1)+"]:"+result[i]+"<br/>";
                };
                document.getElementById("div_result").innerHTML = text;
                document.getElementById("btn_start").style.display = "";
                document.getElementById("btn_stop").style.display = "none";
            }
    </script>
    </form>
</body>
</html>